package com.xiegk.ticket;

import com.xiegk.utils.RedisLockVersion01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * redis 分布式锁 版本 1
 *
 * @author xgk
 * @date 2022/2/20
 */
@Component
public class TicketRedisLockv01 {
    private int number = 50;
    @Autowired
    private RedisLockVersion01 redisLock;

    public void sale() {
        String key = "ticket:lock";
        redisLock.lock(key, 50);
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "\t" + "卖出第" + (number--) + "\t还剩下：" + number);
            }
        } finally {
            redisLock.unlock(key);
        }
    }
}
